package cn.stylefeng.guns.sys.modular.tao.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.mapper.FileInfoMapper;
import cn.stylefeng.guns.sys.modular.tao.entity.Goods;
import cn.stylefeng.guns.sys.modular.tao.entity.GoodsPic;
import cn.stylefeng.guns.sys.modular.tao.model.params.GoodsParam;
import cn.stylefeng.guns.sys.modular.tao.model.params.GoodsPicParam;
import cn.stylefeng.guns.sys.modular.tao.model.result.GoodsResult;
import cn.stylefeng.guns.sys.modular.tao.service.GoodsPicService;
import cn.stylefeng.guns.sys.modular.tao.service.GoodsService;
import cn.stylefeng.guns.sys.modular.tao.service.PictureVideoService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 商品表控制器
 *
 * @author
 * @Date 2019-08-09 12:07:06
 */
@Controller
@RequestMapping("/goods")
public class GoodsController extends BaseController {

    private static final String SystemConstant = "https://hybn-1259399233.cos.ap-chengdu.myqcloud.com/";


    private String PREFIX = "/goods";

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsPicService goodsPicService;

    //private TaoIndexPicService taoIndexPicService;

    @Autowired
    FileInfoMapper fileInfoMapper;

    /**
     * 图文视频
     */
    @Autowired
    private PictureVideoService pictureVideoService;



    /**
     * 跳转到主页面
     *
     * @author
     * @Date 2019-08-09
     */
    @RequestMapping("")
    public String index(HttpSession session) {
        session.removeAttribute("multipleGraph");
        return PREFIX + "/goods.html";
    }

    /**
     * 新增页面
     *
     * @author
     * @Date 2019-08-09
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/goods_add.html";
    }

    /**
     * 编辑页面
     *
     * @author
     * @Date 2019-08-09
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/goodsPic_edit.html";
    }

    /**
     * 编辑页面
     *
     * @author
     * @Date 2019-08-09
     */
    @RequestMapping("/details")
    public String details() {
        return PREFIX + "/goods_detail.html";
    }

    /**
     * 新增接口
     *
     * @author
     * @Date 2019-08-09
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(GoodsParam goodsParam) {
        this.goodsService.add(goodsParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author
     * @Date 2019-08-09
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(GoodsParam goodsParam) {
        goodsParam.setUpdateTime(new Date());
        goodsService.updateType(goodsParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author
     * @Date 2019-08-09
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(GoodsParam goodsParam) {
        this.goodsService.delete(goodsParam);
        return ResponseData.success();
    }



    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(GoodsParam goodsParam) {
        Goods detail = this.goodsService.getById(goodsParam.getId());
        return ResponseData.success(detail);
    }


    /**
     * 查看详情接口
     */
    @RequestMapping("/detailA")
    @ResponseBody
    public ResponseData details(GoodsPicParam goodsPicParam, GoodsParam goodsParam, HttpSession session) {
        GoodsPic byGoodsId = goodsPicService.getByGoodsId(goodsParam.getId().toString());
        //如果这个商品在商品图片表中有数据就显示
        if(byGoodsId!=null){
            session.setAttribute("goodsPicParamId",byGoodsId.getId());
            return ResponseData.success(byGoodsId);
        }else{
            goodsPicParam.setGoodsId(goodsParam.getId().toString());
            goodsPicService.add(goodsPicParam);
            //否则就返回该商品的ID
            Map<String,String> maps = new HashMap<>();
            maps.put("goodsId",goodsParam.getId().toString());
            return ResponseData.success(maps);
        }
    }

    /**
     * 查询列表
     *
     * @author
     * @Date 2019-08-09
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(GoodsParam goodsParam) {
        LayuiPageInfo pageBySpec = this.goodsService.findPageBySpec(goodsParam);
        List<GoodsResult> data = pageBySpec.getData();
        pageBySpec.setData(data);
            return pageBySpec;
    }

    /**
     * 添加热门商品
     *
     * @author
     * @Date 2019-08-09
     */
    /*@RequestMapping("/addHot")
    @ResponseBody
    public ResponseData addHot(TaoIndexPicParam taoIndexPicParam) {
        //Date date=new Date();
        taoIndexPicService.addHotGood(taoIndexPicParam);
        return ResponseData.success();
    }*/





    /**
     * 跳转到图文视频页面
     */
    @RequestMapping("/pictureVideoHtml")
    public String pictureVideoHtml(String id, HttpSession session) {
        /**
         * 商品表 id
         */
        session.setAttribute("goodsId",id);
        return  "pictureVideo/pictureVideo_edit.html";
    }






    /**
     * 商品批量上架
     */
    @RequestMapping("/updateBatchShelves")
    @ResponseBody
    public ResponseData updateBatchShelves(String id) {
        GoodsParam goodsParam = new GoodsParam();
        String[] strArr = id.split(",");
        for (int i = 0; i < strArr.length; ++i){
            if(strArr[i] != null && strArr[i] != ""){
                goodsParam.setUpdateTime(new Date());
                goodsParam.setState(1);
                goodsParam.setId(Long.parseLong(strArr[i]));
                goodsService.updateType(goodsParam);
                System.out.println();
            }
        }
        return ResponseData.success();
    }

    /**
     * 商品批量下架
     */
    @RequestMapping("/updateBatchOffTheShelf")
    @ResponseBody
    public ResponseData updateBatchOffTheShelf(String id) {
        GoodsParam goodsParam = new GoodsParam();
        String[] strArr = id.split(",");
        for (int i = 0; i < strArr.length; ++i){
            System.out.println();
            if(strArr[i] != null && strArr[i] != ""){
                goodsParam.setUpdateTime(new Date());
                goodsParam.setState(2);
                goodsParam.setId(Long.parseLong(strArr[i]));
                goodsService.updateType(goodsParam);
            }
        }
        return ResponseData.success();
    }









}


